﻿using DrawControl.UI.Common;
using DrawControl.UI.View;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DrawControl.UI
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            SetStyle(
         ControlStyles.ResizeRedraw
         | ControlStyles.DoubleBuffer
         | ControlStyles.UserPaint
         | ControlStyles.AllPaintingInWmPaint
         | ControlStyles.SupportsTransparentBackColor,
         true
         );

            this.viewControl1.MouseDownCallback += ViewControl_MouseDownCallback;
            this.viewControl2.MouseDownCallback += ViewControl_MouseDownCallback;
        }

        private void ViewControl_MouseDownCallback(object arg1, MouseEventArgs arg2)
        {
            panel1.DoDragDrop(arg1 as ViewControl, DragDropEffects.Move| DragDropEffects.Copy);
        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiCreation_Click(object sender, EventArgs e)
        {
            using OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Title = "选择图像";
            openFileDialog.Filter = "图像文件(*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                FrmImage frmImage = new FrmImage(openFileDialog.FileName);
                frmImage.Tag = Guid.NewGuid().ToString();
                frmImage.CloseCallback = CloseTab;
                frmImage.SettingFormCallback = SettingForm;
                TabPage tabPage = new TabPage();
                tabPage.Tag = frmImage.Tag;
                tabPage.Text = Path.GetFileName(openFileDialog.FileName);
                tabPage.ImageIndex = 0;
                frmImage.EmbedFormToControl(tabPage);
                this.tabControl1.TabPages.Add(tabPage);
                this.tabControl1.SelectedTab = tabPage;
            }
        }
        private void CloseTab(FrmImage frmImage)
        {
            foreach (TabPage tabPage in this.tabControl1.TabPages)
            {
                if (tabPage.Tag.ToString() == frmImage.Tag.ToString())
                {
                    this.tabControl1.TabPages.Remove(tabPage);
                    break;
                }
            }
            frmImage.Close();
        }
        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsmiExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void panel1_DragEnter(object sender, DragEventArgs e)
        {
            e.Effect = DragDropEffects.Move | DragDropEffects.Copy;
        }
        private void SettingForm(Form form)
        {
            form.EmbedFormToControl(this.panel3);
        }
    }
}
